package com.xy.smartplatfrom.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * projectName:smartplatfrom
 * author:xiaoyang
 * time:2021/12/27 21:11
 * description:
 */
@Configuration
public class BeanConfig {

     @Bean
    public DataSource source(){
         DruidDataSource druidDataSource=new DruidDataSource();
         druidDataSource.setUsername("root");
         druidDataSource.setPassword("zaiyiqiya21");
         druidDataSource.setUrl("jdbc:mysql://localhost:3306/smartplatform?useSSL=false&characterEncoding=utf-8");
         druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
  return druidDataSource;

    }



    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource source){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(source);
        org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);//开启驼峰命名   也就是表中的a_b的列会封装到aB属性
        configuration.setLogImpl(StdOutImpl.class); //打印sql
        sqlSessionFactoryBean.setConfiguration(configuration);
        PageInterceptor pageInterceptor=new PageInterceptor();  //分页的插件
        sqlSessionFactoryBean.setPlugins(pageInterceptor);  //设置分页的插件
        return sqlSessionFactoryBean;
    }


    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
   MapperScannerConfigurer configurer=new MapperScannerConfigurer();
   configurer.setBasePackage("com.xy.smartplatfrom.mapper");
   return configurer;
    }


      @Bean
    public PlatformTransactionManager transactionManager(){
          DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager();
          dataSourceTransactionManager.setDataSource(source());
          return dataSourceTransactionManager;
      }
}
